package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.SysWorkLine;
import com.ruoyi.system.service.ISysClientService;
import com.ruoyi.system.service.ISysWorkLineService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysClientData;
import com.ruoyi.system.service.ISysClientDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产线运行记录Controller
 * 
 * @author spark
 * @date 2023-08-19
 */
@RestController
@RequestMapping("/system/data")
public class SysClientDataController extends BaseController
{
    @Autowired
    private ISysClientDataService sysClientDataService;

    @Autowired
    private ISysWorkLineService sysWorkLineService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询产线运行记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysClientData sysClientData)
    {
        startPage();
        List<SysClientData> list = sysClientDataService.selectSysClientDataList(sysClientData);
        return getDataTable(list);
    }

    /**
     * 查询产线实时状态
     */
    @PreAuthorize("@ss.hasPermi('system:data:listStatus')")
    @GetMapping("/listStatus")
    public TableDataInfo listStatus(SysClientData sysClientData)
    {
        startPage();
        SysWorkLine sysWorkLine = new SysWorkLine();
        if(sysClientData!=null){
            sysWorkLine.setRoomId(sysClientData.getRoomId());
            sysWorkLine.setWorkLineId(sysClientData.getWorkLineId());
            sysWorkLine.setFactoryId(sysClientData.getFactoryId());
            sysWorkLine.setParams(sysClientData.getParams());
        }
        List<SysWorkLine> lists = sysWorkLineService.selectSysWorkLineList(sysWorkLine);
        List<SysClientData> listss = lists.stream().map(x->{
            SysClientData sysClientData1 = new SysClientData();
            sysClientData1.setWorkLineId(x.getWorkLineId());
            sysClientData1.setLineName(x.getLineName());
            sysClientData1.setLineCode(x.getLineCode());
            sysClientData1.setFactoryId(x.getFactoryId());
            sysClientData1.setFactoryName(x.getFactoryName());
            sysClientData1.setRoomId(x.getRoomId());
            sysClientData1.setRoomName(x.getRoomName());
            SysClientData cacheObject = (SysClientData)redisCache.getCacheObject(CacheConstants.WORK_LIEN_ID_STATUS + x.getWorkLineId());
            if(cacheObject!=null){
                sysClientData1.setDataState(cacheObject.getDataState());
                sysClientData1.setDataStartTime(cacheObject.getDataStartTime());
                sysClientData1.setDataEndTime(cacheObject.getDataEndTime());
                sysClientData1.setDurationTime(cacheObject.getDurationTime());
            }else{
                sysClientData1.setDataState("2");
            }
            return sysClientData1;
        }).collect(Collectors.toList());
        //List<SysClientData> list = sysClientDataService.selectSysClientDataList(sysClientData);
        return getDataTable(listss);
    }

    /**
     * 导出产线运行记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:data:export')")
    @Log(title = "产线运行记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysClientData sysClientData)
    {
        List<SysClientData> list = sysClientDataService.selectSysClientDataList(sysClientData);
        ExcelUtil<SysClientData> util = new ExcelUtil<SysClientData>(SysClientData.class);
        util.exportExcel(response, list, "产线运行记录数据");
    }

    /**
     * 获取产线运行记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:data:query')")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return success(sysClientDataService.selectSysClientDataByDataId(dataId));
    }

    /**
     * 新增产线运行记录
     */
    @PreAuthorize("@ss.hasPermi('system:data:add')")
    @Log(title = "产线运行记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysClientData sysClientData)
    {
        return toAjax(sysClientDataService.insertSysClientData(sysClientData));
    }

    /**
     * 修改产线运行记录
     */
    @PreAuthorize("@ss.hasPermi('system:data:edit')")
    @Log(title = "产线运行记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysClientData sysClientData)
    {
        return toAjax(sysClientDataService.updateSysClientData(sysClientData));
    }

    /**
     * 删除产线运行记录
     */
    @PreAuthorize("@ss.hasPermi('system:data:remove')")
    @Log(title = "产线运行记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable Long[] dataIds)
    {
        return toAjax(sysClientDataService.deleteSysClientDataByDataIds(dataIds));
    }
}
